package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.ajax.AjaxRes;
import com.common.utils.MD5Util;
import com.controller.base.BaseController;
import com.entity.YongHu;
import com.service.YongHuService;

@Controller
public class LoginController extends BaseController<Object>{
	@Autowired
	private YongHuService yhService;
	
	@RequestMapping("/login")
	public String login(Model md){
		return "login";
	}
	

	@RequestMapping("/to_login")
	@ResponseBody
	public AjaxRes toLogin(YongHu o,HttpServletRequest req){
		AjaxRes res = new AjaxRes();
		String pwdMD5 = o.getPwd();
		o.setPwd(MD5Util.md5(pwdMD5));
		int len = yhService.countPWD(o);
		if (len<=0) {
			res.setFailMsg("登录失败");
			return res;
		}
		res.setSucceedMsg("登录成功");
		List<YongHu> yhList = yhService.find(o);
		req.getSession().setAttribute("USER", yhList.get(0));
		return res;
	}
	

	@RequestMapping("/to_logout")
	public String toLoginOut(YongHu o,HttpServletRequest req){
		HttpSession userSession = req.getSession();
		userSession.setAttribute("USER", null);
		return "login";
	}
}
